﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Sanford.Multimedia.Midi;
using System.Diagnostics;
using System.Windows.Threading;
using System.Timers;
using System.Collections.ObjectModel;
using WpfHero.HeroService;

namespace WpfHero
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public WHEngine Engine { get; set; }

        private Storyboard _showLoading;
        private Storyboard _startPlaying;
        private bool _storyboardsInitialized = false;

        public Window1()
        {
            InitializeComponent();
            Engine = DataContext as WHEngine;
            _showLoading = (Storyboard)Resources["ShowLoading"];
            _startPlaying = (Storyboard)Resources["StartPlaying"];

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "Midi files|*.mid";
            ofd.Multiselect = false;
            bool? result = ofd.ShowDialog();
            if (result.HasValue && result.Value)
            {
                var path = System.IO.Path.GetDirectoryName(ofd.FileName);
                StartGame(path);
            }
        }

        private void StartGame(string path)
        {
            Engine.Stop();

            if (!_storyboardsInitialized)
            {
                Engine.SongFinished += SongFinished;
                _showLoading.Completed += delegate
                {
                    Engine.Initialize(path, 1);
                    //TODO: startplaying does not play
                    App.DoEvents();
                    _startPlaying.Begin(this);
                };
                _startPlaying.Completed += delegate
                {
                    scroller.Start();
                    Engine.Start();
                };
                _storyboardsInitialized = true;
            }
            _showLoading.Begin(this);
        }

        private void SongFinished(object sender, EventArgs e)
        {
            var client = new HeroServiceSoapClient("HeroServiceSoap");
            client.UploadResult("player", Engine.Score, DateTime.Now, 1, 1);
        }

        private void lbSongBrowser_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SongBrowserDirSelected();
        }

        private void lbSongBrowser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SongBrowserDirSelected();
        }

        private void SongBrowserDirSelected()
        {
            SongBrowser sb = (SongBrowser) ((ObjectDataProvider) Resources["SongBrowserDS"]).ObjectInstance;
            var selectedDir = (MyDirectory) lbSongBrowser.SelectedItem;
            sb.CurrentDirectory = selectedDir;

            if (selectedDir.HasSong)
                StartGame(selectedDir.FullPath);
        }
    }
}
